import * as proxy from '../proxy';
import ServiceBase from '../base';
import { UPLOAD_REQUEST_TIMEOUT } from '../context/context-base';

class UploadService extends ServiceBase {
    uploadVersion = data => {
        let promise = super.put({
            url: `/projects/${data.projectId}/file/${data.fileId}/versions/${data.version}`,
            data: data.data,
            headers: { 'Content-Type': 'multipart/form-data' },
        });
        return proxy.httpResultProxy(promise);
    };
    deleteAppFile = data => {
        let promise = super.delete({
            url: `/projects/${data.projectId}/file/${data.fileId}/appData`,
            params: data.data,
        });
        return proxy.httpResultProxy(promise);
    };
    uploadFilePut = data => {
        let promise = super.put({
            url: data.data.action,
            data: data.file,
            headers: data.data.headers,
            timeout: UPLOAD_REQUEST_TIMEOUT,
            cancelToken: data.cancelToken,
            onUploadProgress: data.onUploadProgress,
        });
        return proxy.httpResultProxy(promise);
    };
    uploadFilePost = data => {
        let promise = super.post({
            url: data.data.action,
            data: data.file,
            headers: data.data.headers,
            timeout: UPLOAD_REQUEST_TIMEOUT,
            cancelToken: data.cancelToken,
            onUploadProgress: data.onUploadProgress,
        });
        return proxy.httpResultProxy(promise);
    };
    uploadAppFilePost = data => {
        let promise = super.post({
            url: data.data.action,
            data: data.file,
            headers: data.data.headers,
            timeout: UPLOAD_REQUEST_TIMEOUT,
        });
        return proxy.httpResultProxy(promise);
    };
    uploadAppFile = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/apps/${data.appId}/folder/${data.folderId}/file?fileName=${data.fileName}`,
            data: JSON.stringify(data.data),
            headers: { 'Content-Type': 'application/octet-stream' },
        });
        return proxy.httpResultProxy(promise);
    };
    getAppFile = data => {
        let promise = super.get({
            url: `/projects/${data.projectId}/file/${data.fileId}`,
        });
        return proxy.httpResultProxy(promise);
    };
    getAppFileVersion = data => {
        let promise = super.get({
            url: `/projects/${data.projectId}/file/${data.fileId}/versions/${data.version}`,
        });
        return proxy.httpResultProxy(promise);
    };
    getUploadUrl = data => {
        let promise = super.get({
            url: `/uploadUrl`,
            params: data,
        });
        return proxy.httpResultProxy(promise);
    };
    getFolder = data => {
        // GET /projects/{projectId}/folder/{folderId}
        let promise = super.get({
            url: `/projects/${data.projectId}/folder/${data.folderId}`,
        });
        return proxy.httpResultProxy(promise);
    };
    // POST /projects/{projectId}/file/multipart/init 初始化分片上传
    initMultipart = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/file/multipart/init`,
            cancelToken: data.cancelToken,
        });
        return proxy.httpResultProxy(promise);
    };
    // POST /projects/{projectId}/file/multipart/upload 逐个上传分片文件
    uploadMultipart = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/file/multipart/upload`,
            data: data.data,
            params: data.params,
            timeout: UPLOAD_REQUEST_TIMEOUT,
            cancelToken: data.cancelToken,
            onUploadProgress: data.onUploadProgress,
        });
        return proxy.httpResultProxy(promise);
    };
    // POST /projects/{projectId}/file/multipart/complete 完成分片上传文件
    completeMultipart = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/file/multipart/complete`,
            data: data.data,
            timeout: data.requestTimeout,
            cancelToken: data.cancelToken,
        });
        return proxy.httpResultProxy(promise);
    };

    public uploadFont = (data: any) => {
        const promise = super.post({
            url: '/fonts',
            data: data.file,
            headers: { 'Content-Type': 'multipart/form-data' },
            cancelToken: data.cancelToken,
            onUploadProgress: data.onUploadProgress,
            timeout: UPLOAD_REQUEST_TIMEOUT,
        });
        return proxy.httpResultProxy(promise);
    };

    // POST /projects/{projectId}/delta/init 初始化增量上传
    public initDeltaUpload = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/delta/init`,
            params: data.params,
            cancelToken: data.cancelToken,
        });
        return proxy.httpResultProxy(promise);
    };

    // GET /projects/{projectId}/file/delta 获取分片信息
    public getDeltaInfo = data => {
        let promise = super.get({
            url: `/projects/${data.projectId}/file/delta`,
            params: data.params,
        });
        return proxy.httpResultProxy(promise);
    };

    // POST /projects/{projectId}/file/delta/upload 逐个上传分片文件
    public uploadDeltaPart = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/file/delta/upload`,
            data: data.data,
            params: data.params,
            timeout: UPLOAD_REQUEST_TIMEOUT,
            cancelToken: data.cancelToken,
            onUploadProgress: data.onUploadProgress,
        });
        return proxy.httpResultProxy(promise);
    };

    // POST /projects/{projectId}/file/delta/complete 完成增量上传
    public completeDeltaUpload = data => {
        let promise = super.post({
            url: `/projects/${data.projectId}/file/delta/complete`,
            data: data.data,
            params: data.params,
            headers: { 'Content-Type': 'multipart/form-data' },
            timeout: data.requestTimeout,
            cancelToken: data.cancelToken,
        });
        return proxy.httpResultProxy(promise);
    };
}

export { UploadService };
